package com.itheima.dianming.config;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.dianming.mapper.StuMapper;
import com.itheima.dianming.pojo.domain.Stu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
    private String type;

    @Autowired
    private StuMapper stuMapper;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
                                        Authentication authentication) throws IOException, ServletException {
        logger.info("登录成功");
        //返回json格式数据
        response.setContentType("application/json;charset=UTF-8");
        String name = authentication.getName();
        LambdaQueryWrapper<Stu> qw = new LambdaQueryWrapper<>();
        qw.eq(Stu::getNumber,name);
        Stu stu = stuMapper.selectOne(qw);
        response.getWriter().write("{\"flag\":true,\"data\":\"登录成功\",\"name\":\""+stu.getUsername()+"\"}");

    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }

}
